#include <bits/stdc++.h>
using namespace std;
class Solution
{
public:
    int totalFruit(vector<int> &fruits)
    {
        int hash[100010] = {0};
        int n = fruits.size();
        int a = 0, b = 0, ans = 0, cnt = 0;
        for (int l = 0, r = 0; r < n; r++)
        {
            if (hash[fruits[r]])
            {
                hash[fruits[r]]++;
            }
            else
            {
                hash[fruits[r]]++;
                cnt++;
            }

            while (cnt > 2)
            {
                hash[fruits[l]]--;
                if (!hash[fruits[l]])
                    cnt--;
                l++;
            }
            ans = max(ans, r - l + 1);
        }
        return ans;
    }
};